
ExtGames.HangoutUtil = function(config) {
	this.participants = [];
	this.turnId = '';
	this.myId = '';
}

ExtGames.HangoutUtil.prototype = {

	initialSetup: function(pack) {
		var participants = gapi.hangout.getEnabledParticipants();
				
		var id = gapi.hangout.getParticipantId();
				
		var delta = {};
		delta['turn'] = '0';
		delta['turnId'] = id;
		delta['firstPlayer'] = id;
		delta['participants'] = JSON.stringify(participants);
		delta['deck'] = pack.getCardString();
		gapi.hangout.data.submitDelta(delta);
	},
	
	 nextRound: function() {
		
		var opp = this.getOpponents();
		
		var firstP = gapi.hangout.data.getState()['firstPlayer'];
		var ids = [];
		ids.push(gapi.hangout.getParticipantId());
		for (var i = 0; i < opp.length; i++) {
			ids.push(opp[i].id);
		}
		
		var index = ids.indexOf(firstP);
		index = (index+1)%ids.length;
		firstP = ids[index];
		
		var delta = {};
		delta['turn'] = '0';
		delta['turnId'] = firstP;
		delta['firstPlayer'] = firstP;
		delta['deck'] = game.pack.getCardString();
		gapi.hangout.data.submitDelta(delta);
	},
	
	submitNextRound: function() {
		var delta = {};
		delta['nextRound'] = ''+(game.round+1);
		gapi.hangout.data.submitDelta(delta);
	},
	
	 endTurn: function() {
		
		var nextOpp = this.getNextOpponent();
		
		var delta = {};
		delta['turn'] = ''+nextOpp['turn'];
		delta['turnId'] = nextOpp['id'];
		gapi.hangout.data.submitDelta(delta);
	},
	
	 getTurnName: function() {
		return getName(this.getTurnId());
	},
	
	 isTurn: function() {
	    var id = gapi.hangout.getParticipantId();
	    return (this.getTurnId() == id);
	},
	
	getTurnId: function() {
		if (!this.turnId || this.turnId == '') {
			return gapi.hangout.data.getState()['turnId'];
		}
		return this.turnId;
	},
	
	 getTurnIndex: function() {
		var participants = this.getParticipants();
		
		var i = 0;
		while (participants[i].id != gapi.hangout.getParticipantId()) {
			i++;
		}
				
		var turnId = this.getTurnId();
		var j = 0;
		while (participants[j].id != turnId) {
			j++;
		}
	    
	    var index = (j - i);
	    if (index < 0) {
	    	index += this.getParticipants().length;
	    }
	    
	    return index;
	},
	
	 drawCard: function(details) {
			
		details.id = gapi.hangout.getParticipantId();
		details.sourceId = gapi.hangout.getParticipantId();
		
		gapi.hangout.data.submitDelta({
			draw: JSON.stringify(details)
		});
	},
	
	 discard: function(details) {
		
		details.id = gapi.hangout.getParticipantId();
		
		gapi.hangout.data.submitDelta({
			discard: JSON.stringify(details)
		});
	},
	
	 getNextOpponent: function() {
		var participants = this.getParticipants();
		
		var i = 0;
		while (participants[i].id != gapi.hangout.getParticipantId()) {
			i++;
		}
		i++;
		
		if (i >= participants.length) {
			i = 0;
		}
		
		return {
			turn: i,
			id: participants[i].id
		}
	},
	
	 getOpponentIndex: function(id) {
		var opp = this.getOpponents();
		for (var i = 0; i < opp.length; i++) {
			if (opp[i].id == id) {
				return (i+1);
			}
		}
	},
	
	getParticipants: function() {
		if (!this.participants || this.participants.length == 0) {
			this.participants = eval('('+gapi.hangout.data.getState()['participants']+')');
		}
		return this.participants;
	},
	
	getParticipant: function(id) {
		var participants = this.getParticipants();
		for (var i = 0; i < participants.length; i++) {
			console.log(participants[i].id);
			if (participants[i].id == id) {
				return participants[i];
			}
		}
	},
	
	 getOpponents: function() {
		 		 
		var participants = this.getParticipants();
		
		var i = 0;
		var myId = gapi.hangout.getParticipantId();
		while (participants[i].id != myId) {
			i++;
		}
		i++;
		var opp = [];
				
		for (var j = 0; j < (participants.length-1); j++) {
			if (i >= participants.length) {
				i = 0;
			}
			
			opp[j] = {
				id:	participants[i].id,
				name: participants[i].person.displayName
			};
			
			i++;
		}
				
		return opp;
	},
	
	 getMyId: function() {
	 	 return gapi.hangout.getParticipantId();
	},
	
	 getMe: function() {
		return this.getParticipant(gapi.hangout.getParticipantId());
	},
	
	 getName: function(id) {
		return this.getParticipant(id).person.displayName;
	},
	
	 getNumP: function() {
		return this.getParticipants().length;
	},
	
	getTurnName: function() {
		return this.getName(this.getTurnId());
	},
	
	 submitScores: function(scores) {
		var opp = this.getOpponents();
		
		var scorecard = {};
		scorecard[gapi.hangout.getParticipantId()] = scores[0];
		for (var i = 0; i < opp.length; i++) {
			scorecard[opp[i].id] = scores[i+1];
		}
		
		gapi.hangout.data.submitDelta({
			scores: JSON.stringify(scorecard)
		});
	},
	
	
	
	// Callback s for events created by the hangout
	 apiReady: function() {
	  console.log("API is ready");
	},
	
	
	 stateUpdated: function (stateEvent) {
	
		var adds = stateEvent.addedKeys;
		var removes = stateEvent.removedKeys;
		 
		if (adds != null && adds.length > 0) {
			console.log('Added to state ('+adds.length+'): ');
			for (var i = 0; i < adds.length; i++) {
				console.log("\t"+adds[i].key+": "+adds[i].value);
				
				var added = adds[i];
				
				if (added.key == 'deck') {
					
					if (game == null) {
						selectionWin.close();
						game = new ExtGames.MayIGame({
							cards: added.value
						});		
						game.newGame();
						game.launch();	
						game.nextTurn();
					}
					else {
						game.pack.createCards({
							cards: added.value
						});
					}
					
					//document.getElementById('startDiv').style.display = 'none';
				}
				else if (added.key == 'turnId') {
					if (game != null) {
						this.turnId = added.value;
						game.nextTurn();
					}
				}
				else if (added.key == 'draw') {					
					var details = eval('('+added.value+')');
					console.log(details.sourceId);
					console.log(gapi.hangout.getParticipantId());
					if (details.sourceId != gapi.hangout.getParticipantId()) {
						game.drewCard(details);
					}
				}
				else if (added.key == 'discard') {
					var details = eval('('+added.value+')');
					if (details.id != gapi.hangout.getParticipantId()) {
						game.discardedCard(details);
					}
				}
				else if (added.key == 'scores') {
					game.updateScores(eval('('+added.value+')'));
				}
				else if (added.key == 'firstPlayer') {
					if (game != null && added.value == gapi.hangout.getParticipantId()) {
						game.win.updateStatus('It is your turn. Press the "Next Round" button to start the next round.');
						Ext.getCmp('nextRoundButton').enable();
					}
					
				}
				else if (added.key == 'nextRound') {
					game.reformDeck();
				}
			}
		}
		if (removes != null && removes.length > 0) {
			console.log("Removed from state: ");
			for (var i = 0; i < removes.length; i++) {
				console.log("\t"+removes[i]);
				
			}
			
		}
	},
	
	 participantsUpdated: function(participantsArray) {
	  
	}
	
};



